package com.wh.wisdomsite.xbox.server.controller.us;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.pojo.us.GetUsersOut;
import com.wh.wisdomsite.xbox.pojo.us.HkUserIn;
import com.wh.wisdomsite.xbox.server.util.DateUtils;
import com.wh.wisdomsite.xbox.us.entity.HkUser;
import com.wh.wisdomsite.xbox.us.service.IHkUserService;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil_Org;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 海康用户controller
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.us
 * @date 2018年09月04日 11:19
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/hkUser")
public class HkUserController {
    private static final Log logger = LogFactory.getLog(HkUserController.class);

    @Autowired
    private IHkUserService iHkUserService;

    /**
     * 跳转海康用户列表
     * @return
     */
    @RequiresPermissions("rules:us_hkUser_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(){
        return "/us/hkUser/hkUserList";
    }

    /**
     * 获取海康用户数据列表
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:us_hkUser_find")
    @RequestMapping(value = "/hkUserList.do")
    public void hkUserList(HttpServletResponse response, HkUserIn in){
        try {
            logger.info("加载海康用户数据列表-->>开始");
            //设置分页
            if(in.getPage() == 0) in.setPage(1);
            if(in.getRows() == 0) in.setRows(10);
            Page<HkUser> page = new Page<HkUser>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            EntityWrapper<HkUser> ewHkUser = new EntityWrapper<HkUser>();
            ewHkUser.setEntity(new HkUser());
            if(StringUtils.isNotEmpty(in.getLoginAccount())){
                ewHkUser.like("loginAccount",in.getLoginAccount());
            }
            if(StringUtils.isNotEmpty(in.getUserName())){
                ewHkUser.like("userName",in.getUserName());
            }
            ewHkUser.orderBy("updateTime",false);
            Page<HkUser> hkUserPage = iHkUserService.selectPage(page,ewHkUser);
            JSONObject json = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            if(hkUserPage.getRecords().size() > 0){
                for(HkUser hkUser : hkUserPage.getRecords()){
                    JSONObject object = new JSONObject(hkUser);
                    jsonArray.put(object);
                }
            }
            json.put("total", hkUserPage.getTotal());
            json.put("rows", jsonArray);
            ResManager.getTextOut(response).print(json);
            logger.info("加载海康用户数据列表-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("加载海康用户数据列表-->>Exception" + e.getMessage());
        }
    }

    /**
     * 海康用户详情
     * @param id
     * @param request
     * @return
     */
    @RequiresPermissions("rules:us_hkUser_view")
    @RequestMapping("hkUserDesc.do")
    public String commonFileDesc(String id, HttpServletRequest request) {
        logger.info("海康用户详情-->>开始");
        HkUser hkUser = iHkUserService.selectById(id);
        Long createTime = hkUser.getCreateTime();
        Long updateTime = hkUser.getUpdateTime();
        String createTimeStr = DateUtils.timeChangeToDate(createTime);
        String updateTimeStr = DateUtils.timeChangeToDate(updateTime);
        request.setAttribute("hkUser", hkUser);
        request.setAttribute("createTimeStr", createTimeStr);
        request.setAttribute("updateTimeStr", updateTimeStr);
        logger.info("海康用户详情-->>结束");
        return "us/hkUser/hkUserDesc";
    }

    /**
     * 获取默认用户
     */
    @RequestMapping(value = "/hkUserGetDefualt .do")
    public void hkUserGetDefualt ( ){
        try{
            logger.info("同步海康用户数据-->>开始");
            CommonResult commonResult =  iHkUserService.getDefaultUserUuid();
            System.out.println("date:" + commonResult.getResult());
            logger.info("同步海康用户数据-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("同步海康用户数据-->>Exception" + e.getMessage());
        }
    }

    /**
     * 同步获取用户
     */
    @RequiresPermissions("rules:us_hkUser_syn")
    @RequestMapping(value = "/hkUserSynchronizd.do")
    public void hkUserSynchronizd(HttpServletResponse response){
        try{
            logger.info("同步海康用户数据-->>开始");
            CommonResult commonResult =  iHkUserService.getUsers();
            //获取数据库用户数据
            EntityWrapper<HkUser> ewHkUser = new EntityWrapper<HkUser>();
            ewHkUser.setEntity(new HkUser());
            List<HkUser> hkUsers = iHkUserService.selectList(ewHkUser);
            Map<String,String> hkMap = new HashMap<String,String>();
            if(hkUsers.size() >0){
                for(HkUser hkUser : hkUsers){
                    hkMap.put(hkUser.getUserUuid(),hkUser.getUserName());
                }
            }
            //同步数据
            GetUsersOut usersOut = (GetUsersOut)commonResult.getResult();
            List<HkUser> hkUserList = usersOut.getList();
            if(hkUserList.size() >0){
                for(HkUser hkUser : hkUserList){
                    String userName = hkMap.get(hkUser.getUserUuid());
                    if(StringUtils.isNotEmpty(userName)){
                        iHkUserService.updateById(hkUser);
                    }else{
                        iHkUserService.insert(hkUser);
                    }
                }
            }
            ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("用户同步成功"));
            logger.info("同步海康用户数据-->>结束");
        }catch (Exception e){
            e.printStackTrace();
            ResManager.getJsonOut(response).print(JsonUtil_Org.returnTrue("用户同步失败"));
            logger.debug("同步海康用户数据-->>Exception" + e.getMessage());
        }
    }

    /**
     * 返回海康用户列表
     * @param response
     */
    @RequestMapping("/getHkUserCombo.do")
    public void getHkUserCombo(HttpServletResponse response){
        try{
            logger.info("返回视频用户-->>开始");
            EntityWrapper<HkUser> ewHkUser = new EntityWrapper<HkUser>();
            ewHkUser.setEntity(new HkUser());
            List<HkUser> hkUserList = iHkUserService.selectList(ewHkUser);
            JSONArray jsonArray = new JSONArray();
            if(hkUserList.size() > 0){
                for(HkUser hkUser : hkUserList){
                    JSONObject object = new JSONObject(hkUser);
                    jsonArray.put(object);
                }
            }
            ResManager.getTextOut(response).print(jsonArray);
            logger.info("返回视频用户-->>结束");
        }catch (Exception e){
            e.printStackTrace();
        }
    }







}
